package com.qijian.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.system.domain.Remind;
import com.qijian.system.domain.vo.RemindQuery;
import com.qijian.system.domain.vo.RemindVo;
import com.qijian.system.service.IRemindService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 用户提醒Controller
 *
 * @author qijian
 * @date 2022-08-24
 */
@Api(tags = "系统管理：用户提醒")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/remind")
public class RemindController extends BaseController {

    private final IRemindService remindService;
    /**
     * 查询用户提醒列表
     */
    @PreAuthorize("@ss.hasPermi('system:remind:list')")
    @GetMapping("/list")
    public TableDataInfo list(RemindQuery query, IPage<Remind> page) {
        return getDataTable(remindService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出用户提醒列表
     */
    @PreAuthorize("@ss.hasPermi('system:remind:export')")
    @Log(title = "用户提醒" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(RemindQuery query, HttpServletResponse response) {
        new ExcelUtil<>(Remind. class).exportExcel(response,remindService.list( WhereEntityTool.invoke(query)), "用户提醒数据");
    }

    /**
     * 获取用户提醒详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:remind:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(remindService.getVoById(id));
    }

    /**
     * 新增用户提醒
     */
    @PreAuthorize("@ss.hasPermi('system:remind:add')")
    @Log(title = "用户提醒" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RemindVo vo) {
        return toAjax(remindService.addOrUpdateVo(vo));
    }

    /**
     * 修改用户提醒
     */
    @PreAuthorize("@ss.hasPermi('system:remind:edit')")
    @Log(title = "用户提醒" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RemindVo vo) {
        return toAjax(remindService.addOrUpdateVo(vo));
    }

    /**
     * 删除用户提醒
     */
    @PreAuthorize("@ss.hasPermi('system:remind:remove')")
    @Log(title = "用户提醒" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        ids.forEach(id -> approval(remindService.getById(id)));
        return toAjax(remindService.removeByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('system:remind:list')")
    @GetMapping("/listVo")
    public TableDataInfo list(RemindQuery query) {
        return getDataTable(remindService.getRemindVoList(WhereEntityTool.invoke(query)));
    }

    @GetMapping("/getUsers/{type}")
    public TableDataInfo list(@PathVariable String type) {
        return getDataTable(remindService.getUserByType(type));
    }

}
